<template>
  <div>
    <div class="flex-start">
      <div class="flex-left m-r-20">
        <div class="f-s-16">上月末库存总量（吨）</div>
        <div class="f-s-20 m-t-10 f-w-600 c-blue">
          {{ summary.lastMonthCurrentCapacity | kg2Tons }}
        </div>
      </div>
      <div class="flex-left m-r-20">
        <div class="f-s-16">补液总量（吨）</div>
        <div class="f-s-20 m-t-10 f-w-600 c-blue">
          {{ summary.supplementCapacity | kg2Tons }}
        </div>
      </div>
      <div class="flex-left m-r-20">
        <div class="f-s-16">加注总量（吨）</div>
        <div class="f-s-20 m-t-10 f-w-600 c-blue">
          {{ summary.fillCapacity | kg2Tons }}
        </div>
      </div>
      <div class="flex-left">
        <div class="f-s-16">当前库存总量（吨）</div>
        <div class="f-s-20 m-t-10 f-w-600 c-blue">
          {{ summary.currentCapacity | kg2Tons }}
        </div>
      </div>
    </div>
    <div class="flex-start m-t-20">
      <el-input
        size="small"
        class="m-r-10"
        clearable=""
        style="width: 300px"
        v-model="search.queryStr"
        placeholder="请输入加注机编号或加注站名称"
      />
      <el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="query"
        class="m-l-10"
        :loading="loading"
        >查询
      </el-button>
    </div>
    <el-table
      class="my-table m-t-20"
      :data="data"
      v-loading="loading"
      style="width: 100%"
      border
      v-if="refreshFlag > 0"
    >
      <el-table-column type="index" align="center" label="序号" width="60" />
      <el-table-column prop="macCode" label="加注机编号" min-width="180" />
      <el-table-column label="加注站名称" min-width="180">
        <template v-slot="scope">
          <span :title="scope.row.siteName">{{
            scope.row.siteName | defaultValue
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="容量（吨）" width="200">
        <template v-slot="scope">
          {{ scope.row.totalCapacity | kg2Tons }}
        </template>
      </el-table-column>
      <el-table-column label="理论余量（吨）" width="140">
        <template v-slot="scope">
          <span class="m-r-10">{{ scope.row.currentCapacity | kg2Tons }}</span>
          <auth-wrapper btn-key="inventory-list:calibration">
            <el-button
              size="mini"
              type="primary"
              round
              plain
              @click="calibration(scope.row)"
              >校准</el-button
            >
          </auth-wrapper>
        </template>
      </el-table-column>
      <el-table-column label="传感器余量（吨）" width="150">
        <template v-slot="scope">
          <el-popover
            placement="top-start"
            title="余量"
            width="200"
            trigger="hover"
          >
            <div>
              <div>更新时间</div>
              <div>
                {{ scope.row.sensorHeightLastUpdateTime | defaultValue }}
              </div>
              <div class="m-t-10">液位高度值（cm）</div>
              <div>{{ scope.row.currentHeight | defaultValue }}</div>
            </div>
            <span
              class="m-r-10"
              slot="reference"
              :title="scope.row.sensorCapacity"
              >{{ scope.row.sensorCapacity | kg2Tons }}</span
            >
          </el-popover>
          <el-button
            size="mini"
            type="primary"
            round
            plain
            @click="updateSensorCapacity(scope.row)"
            >余量更新
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="短信通知" width="100">
        <template v-slot="scope">
          <span :title="scope.row.smsPhone">{{
            scope.row.smsPhone | defaultValue
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警阈值（%）" width="150">
        <template v-slot="scope">
          <span :title="scope.row.warnValue">{{
            scope.row.warnValue | defaultValue
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警阈值（%）" width="150">
        <template v-slot="scope">
          <span :title="scope.row.alarmValue">{{
            scope.row.alarmValue | defaultValue
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="220">
        <template v-slot="scope">
          <auth-wrapper btn-key="inventory-list:settings">
            <el-button
              @click="marginSettings(scope.row)"
              type="text"
              size="small"
              icon="el-icon-edit"
              >库存设置
            </el-button>
          </auth-wrapper>
          <auth-wrapper btn-key="inventory-list:history">
            <el-button
              @click="marginHistory(scope.row)"
              type="text"
              size="small"
              icon="el-icon-document"
              >余量历史
            </el-button>
          </auth-wrapper>
        </template>
      </el-table-column>
    </el-table>
    <app-pagination
      ref="paginationRef"
      :total="total"
      class="m-t-20"
      @do-query="query"
    />

    <el-dialog
      title="余量历史"
      append-to-body
      :visible.sync="showHistoryFlag"
      width="1000px"
    >
      <margin-history-list-content
        :info="currRow"
        v-if="showHistoryFlag"
        @back="showHistoryFlag = false"
      />
    </el-dialog>

    <el-dialog
      title="余量校准"
      append-to-body
      :visible.sync="showCalibrationFlag"
      width="400px"
    >
      <div class="pa-12 flex-start">
        <div style="width: 100px">理论余量</div>
        <el-input-number
          :step="1"
          :min="0"
          class="m-l-10"
          v-model="adjustForm.storageAfter"
        ></el-input-number>
        <div class="m-l-10">kg</div>
      </div>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button
          type="primary"
          size="small"
          :loading="adjustLoading"
          @click="doAdjust"
          >确定</el-button
        >
        <el-button
          type="primary"
          size="small"
          plain
          @click="showCalibrationFlag = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AppPagination from "@/components/business/app-pagination";
import MarginHistoryListContent from "@/views/inventory/inventory-list/components/margin-history-list-content";
import {
  getRecords,
  getRespData,
  getTotal,
  isNull,
  msgSuccess,
} from "@/util/common-util";
import {
  summarymachinestoragewater as summary,
  getPagemachinestoragewater as getPage,
  adjust,
  updateSensorCapacity,
} from "@/api/business/filling-machine";
import { deepClone } from "@/util/util";
import AuthWrapper from "@/components/business/auth-wrapper";
import { kg2Tons } from "@/components/business/global-filter";

export default {
  name: "inventory-list",
  components: { AuthWrapper, MarginHistoryListContent, AppPagination },
  data() {
    return {
      refreshFlag: 1,
      showSearchFlag: true,
      showHistoryFlag: false,
      showCalibrationFlag: false,
      data: [],
      loading: false,
      adjustLoading: false,
      search: {
        partnerId: "",
        clusterId: "",
        type: "",
        status: "",
        queryStr: "",
        isStorageList: true,
      },
      total: 0,
      currRow: {},
      summary: {},
      adjustForm: {
        orderType: 2,
        storageAfter: "",
        machineId: "",
        // changeDesc: '校准'
      },
    };
  },
  mounted() {
    this.query();
    this.getSummary();
  },
  methods: {
    getSummary() {
      summary().then((res) => {
        this.summary = getRespData(res);
      });
    },
    query(_page) {
      this.loading = true;
      if (isNull(_page)) {
        _page = this.$refs.paginationRef.getPage();
      }
      getPage(_page, this.search)
        .then((res) => {
          this.data = getRecords(res);
          this.total = getTotal(res);
          this.loading = false;
        })
        .then(() => {
          this.loading = false;
        });
    },
    marginSettings(_row) {
      this.currRow = _row;
      this.$emit("margin-settings", this.currRow);
    },
    marginHistory(_row) {
      this.currRow = _row;
      this.showHistoryFlag = true;
    },
    calibration(_row) {
      this.currRow = _row;
      this.showCalibrationFlag = true;
      // this.adjustForm.storageAfter = kg2Tons(this.currRow.currentCapacity)
      this.adjustForm.storageAfter = this.currRow.currentCapacity;
    },
    updateSensorCapacity(_row) {
      msgSuccess("更新中");
      updateSensorCapacity(_row.id).then((res) => {
        _row.sensorHeightLastUpdateTime =
          getRespData(res).sensorHeightLastUpdateTime;
        _row.sensorCapacity = getRespData(res).sensorCapacity;
        _row.currentHeight = getRespData(res).currentHeight;
        msgSuccess();
        this.forceRefresh("更新成功");
      });
    },
    forceRefresh() {
      this.refreshFlag = 0;
      this.$nextTick(() => {
        this.refreshFlag = 1;
      });
    },
    doAdjust() {
      this.adjustLoading = true;
      this.adjustForm.machineId = this.currRow.id;
      // todo tons
      const form = deepClone(this.adjustForm);
      // form.storageAfter = form.storageAfter * 1000
      adjust(form)
        .then((res) => {
          this.adjustLoading = false;
          this.showCalibrationFlag = false;
          msgSuccess();
          this.query();
        })
        .catch(() => {
          this.adjustLoading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.my-table {
  ::v-deep .el-button--mini {
    font-size: 12px !important;
    padding: 4px 10px !important;
  }

  ::v-deep .el-button--small {
    padding: 0 5px !important;
    font-size: 14px !important;
  }

  ::v-deep td {
    padding: 8px 0 !important;
  }
}
</style>
